<?php
/**
 * Created by PhpStorm.
 * User: PHP
 * Date: 2021/6/7
 * Time: 17:50
 */
namespace App\Services\notice;
use App\Models\notice\NoticeModel;
use App\Services\BaseServices;
use App\Dao\notice\NoticeDao;
use App\Exceptions\ApiException as HOException;
use App\Services\system\SystemLogServices;
use Hlcreation\Services\AnnouncementServer;
class NoticeServices extends BaseServices
{

    protected $dao;

    public function __construct(NoticeDao $dao,AnnouncementServer $annount)
    {
        $this->dao = $dao;
        $this->an = $annount;

    }
    //公告列表
    public function getList($where=array()){

        return $this->dao->NoticeList($where);
    }


    //添加公告
    public function NociteSave($data){
        //dd($data);
        $addId = $this->dao->create($data);
        if($addId){
            //
             if($data['user_type']!=1){
                 $this->an->annount_send(['title'=>$data['title'],'content'=>$data['synopsis'],'created_at'=>date("Y-m-d H:i:s"),'notice_type'=>$data['notice_type'],'notice_id'=>$addId]);
             }

            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($data['creator_uid'],'添加公告','公告标题:'.$data['synopsis']);
            return true;
        }else{
            return false;
        }


    }


    //编辑公告
    public function NociteEdit($where,$data){
       $id = $where['id'];

        if($this->dao->update(['id'=>$id],$data)){
            $admin_uid=$data['creator_uid'];
            unset($data['creator_uid']);
            $data['notice_id'] = $id;

            if($data['user_type']!=1){

                $this->an->annount_edit($data);
            }
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($admin_uid,'修改公告','公告ID:'.$where['id'].'--修改公告标题:'.$data['title']);
            return true;
        }else{
            return false;
        }
    }


    //删除公告
    public function NociteDel($admin_uid,$id){
        if (!$id) throw new HOException("公告ID不能为空、必须传递!");

        if(!$this->dao->delete(['id' => $id])) {
            return false;
        } else {
            $user_type = NoticeModel::where('id',$id['id'])->value('user_type');

            if($user_type != 1){
                $this->an->annount_del($id['id']);
            }
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($admin_uid, '删除公告', '公告ID:' . $id['id']);
            return true;
        }
    }


    //公告详情
    public function NociteInfo($id){
        if (!$id) throw new HOException("公告ID不能为空、必须传递!");
        return $this->dao->getOne(['id'=>$id]);
    }


    //显示代理商公告
    public function red_notice($userId){

        return  $this->dao->red_notice($userId);

    }


    //已读公告
    public function read($all){
        if (!$all['notice_id']) throw new HOException("公告ID不能为空、必须传递!");

        return $this->dao->read($all);

    }

    //一看公告人数
    public function read_person($notice_id){
        if (!$notice_id) throw new HOException("公告ID不能为空、必须传递!");
        return $this->dao->read_person($notice_id);
    }



















}
